package com.mall.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mall.db.domain.Region;
import com.mall.db.service.RegionService;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 行政区域表 前端控制器
 * </p>
 *
 * @author wxd
 * @since 2021-09-18
 */
@Controller
@RequestMapping("/system/rc")
public class RegionController extends BaseController<Region>{

    @Resource(name = "regionService")
    private RegionService rs;

    @Override
    public Map<String, Object> find(Region r) {

        PageHelper.startPage(r.getPage(),r.getLimit());

        QueryWrapper<Region> qw = new QueryWrapper<>();

        if (!StringUtils.isEmpty(r.getType())){
            qw.eq("type",r.getType());
        }
        if (!StringUtils.isEmpty(r.getName())){
            qw.eq("name",r.getName());
        }
        if (!StringUtils.isEmpty(r.getPid())){
            qw.eq("pid",r.getPid());
        }
        List<Region> list = rs.list(qw);

        PageInfo<Region> page = new PageInfo<>(list,r.getLimit());


        return  this.findMap(page.getList(),page.getTotal());
    }

    @RequestMapping("/findAll")
    public Map<String,Object> findAll(Region r){

        QueryWrapper<Region> qw = new QueryWrapper<>();

        List<Region> list = rs.list(qw);

        return this.findMap(list,list.size());
    }
}

